package com.example.fszq.volleydemo1.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.example.fszq.volleydemo1.R;
import com.example.fszq.volleydemo1.utils.TopicList;

import java.util.List;

/**
 * Created by fszq on 2015/12/8.
 */
public class TopicListAdapter extends ArrayAdapter<TopicList> {
    private int resourceId;

    public TopicListAdapter(Context context,int TopicViewResourceId,List<TopicList> objects) {
        super(context, TopicViewResourceId, objects);
        resourceId = TopicViewResourceId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TopicList tlist = getItem(position);
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, null);
            viewHolder = new ViewHolder();
            viewHolder.title = (TextView)view.findViewById(R.id.title);
            viewHolder.subject = (TextView)view.findViewById(R.id.subject);
            viewHolder.author = (TextView)view.findViewById(R.id.author);
            viewHolder.submitdate = (TextView)view.findViewById(R.id.submitdate);
            viewHolder.viewreply = (TextView)view.findViewById(R.id.viewreply);
            viewHolder.topicid = (TextView)view.findViewById(R.id.topicid);
            view.setTag(viewHolder); // 将ViewHolder存储在View中
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag(); // 重新获取ViewHolder

        }

        viewHolder.title.setText(tlist.getTitle());
        viewHolder.subject.setText(tlist.getSubject());
        viewHolder.author.setText(tlist.getAuthor());
        viewHolder.submitdate.setText(tlist.getSubmitdate());
        viewHolder.viewreply.setText(tlist.getViewreply());
        viewHolder.topicid.setText(tlist.getTopicid());
        return view;
    }

    class ViewHolder {
        TextView title,subject,author,submitdate,viewreply,topicid;
    }
}

